package com.alks.common.utils.excelUtils.handler;

import com.alibaba.excel.enums.CellDataTypeEnum;
import com.alibaba.excel.metadata.Head;
import com.alibaba.excel.metadata.data.WriteCellData;
import com.alibaba.excel.write.handler.CellWriteHandler;
import com.alibaba.excel.write.metadata.holder.WriteSheetHolder;
import com.alibaba.excel.write.metadata.holder.WriteTableHolder;
import org.apache.poi.ss.usermodel.Cell;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

/**
 * 模版-动态列宽
 * @author MS
 * @since 2024/11/30上午10:36
 */
public class TempAutoWidthHandler implements CellWriteHandler {

    private final List<Integer> columnWidths;
    private final List<Integer> defalutWidths;
    private final List<Integer> addList;
    private Integer firstRowIndex;
    private int passRow;

    public TempAutoWidthHandler(int passRow){
        this.columnWidths = new ArrayList<>(Collections.nCopies(20, 0));
        this.defalutWidths = new ArrayList<>(Collections.nCopies(20, 0));
        this.addList = new ArrayList<>(Collections.nCopies(5, 0));
        this.passRow = passRow;
    }

    @Override
    public void afterCellDispose(WriteSheetHolder writeSheetHolder, WriteTableHolder writeTableHolder, List<WriteCellData<?>> cellDataList, Cell cell, Head head, Integer relativeRowIndex, Boolean isHead) {
        /*获取下标 扩充数组 获取模版初始长度*/
        int index = cell.getColumnIndex();
        int rowIndex = cell.getRowIndex();
        while (columnWidths.size() <= index+1){
            columnWidths.addAll(addList);
            defalutWidths.addAll(addList);
        }
        if (passRow>0) {
            /*跳过单个数据*/
            passRow--;
        } else {
            if (firstRowIndex == null) {
                firstRowIndex = rowIndex;
            }
            if (rowIndex == firstRowIndex) {
                defalutWidths.set(index, cell.getSheet().getColumnWidth(index));
            }
        }
        /*获取数据长度*/
        Integer length = dataLength(cellDataList);
        // 获取当前列宽
        int currentWidth = columnWidths.get(index);
        int defaultWidth = defalutWidths.get(index);
        int newWidth = Math.max(currentWidth, length * 256);
        newWidth = Math.max(defaultWidth, newWidth);
        columnWidths.set(index, newWidth);
        // 更新列宽
        writeSheetHolder.getSheet().setColumnWidth(index, newWidth);
    }

    /**
     * 计算数据长度
     * @param cellDataList 单元格数据
     * @return 数据长度
     */
    private Integer dataLength(List<WriteCellData<?>> cellDataList) {
        WriteCellData<?> cellData = cellDataList.get(0);
        CellDataTypeEnum type = cellData.getType();
        if (type == null) {
            return -1;
        } else {
            switch (type) {
                case STRING:
                    return cellData.getStringValue().getBytes().length + 1;
                case BOOLEAN:
                    return cellData.getBooleanValue().toString().getBytes().length + 1;
                case NUMBER:
                    return cellData.getNumberValue().stripTrailingZeros().toString().getBytes().length + 1;
                default:
                    return -1;
            }
        }
    }
}
